{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d642c0b",
   "metadata": {},
   "source": [
    "# 第六周API作业\n",
    "* 作业描述：\n",
    "> 1.纸币的使用在我国为虚拟货币所替代，许多小孩表示，不知道纸币的模样  \n",
    "> 2.不仅是对人民币的认知，更是对外币的了解和收藏  \n",
    "> 3.真假币的识别逐渐淡出人们的认知。尽管纸质人民币使用大量减少，但纸币的储存/纪念，收集纸币编码靓号，开始为小众人群爱好保留。给予了假币市场一定的可乘之机。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b255e937",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.fce715fcc905b868a82b3568263a5b7e.315360000.1965035492.282335-25904781', 'expires_in': 2592000, 'session_key': '9mzdAvCT48HAoaFGY1UluOkoU6I6wfxDlYgyWuwRWGz5wrud+/YGtrAyQCqzaojTxhdaUjyhC9FAAqn7e0KTtunuK2kqpA==', 'access_token': '24.920ef929241fab8d44743e358fa9a20f.2592000.1652267492.282335-25904781', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '3b006ff1d9412807f895e7e2f2db295d'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'CB24XR0H2NW8VuGw1LqAjmjE',\n",
    "    'client_secret':'GAfFYdoBLSllq0xBx1GhMj4LbYGMajXz'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b2e64c73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.920ef929241fab8d44743e358fa9a20f.2592000.1652267492.282335-25904781'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "token = response.json()[\"access_token\"]\n",
    "token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf1f8b31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/currency\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e2ac724",
   "metadata": {},
   "source": [
    "* 外币的识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f475fd02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': {'currencyName': '法国1955.1.6年版500 Francs纸钞', 'hasdetail': 0},\n",
       " 'log_id': 1513479702816342187}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money_info = everything(token,\"fabi.jpeg\")\n",
    "money_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f10c4c39",
   "metadata": {},
   "source": [
    " * 人民币的识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "37894468",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': {'currencyName': '中国人民币',\n",
       "  'currencyCode': 'CNY',\n",
       "  'currencyDenomination': '1',\n",
       "  'year': '1980年',\n",
       "  'hasdetail': 1},\n",
       " 'log_id': 1513479710917482420}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money_info = everything(token,\"yiyuan.jpeg\")\n",
    "money_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fa210dd",
   "metadata": {},
   "source": [
    "* 假币的识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4a783f21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': {'currencyName': '', 'hasdetail': 0}, 'log_id': 1513479769871450107}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money_info = everything(token,\"jiabi2.jpeg\")\n",
    "money_info\n",
    "## 识别失败\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
